<?php

namespace pmvc\remoting;

use pmvc\util\ClassUtil;
use pmvc\ioc\InitializingObject;
use pmvc\aop\ProxyUtil;

use ReflectionClass;
use Exception;

/**
 * An abstract object for exposing services as
 * remotely accessible.
 */
abstract class RemotingServer
	implements InitializingObject {

	private $serviceInterface;
	private $service;
	private $clazz;

	/**
	 * {@inheritDoc}
	 */
	public function initialize() {
		if (!isset($this->service)) {
			throw new Exception("property sevice is required");
		}
		$this->clazz = new ReflectionClass(
			isset($this->serviceInterface) ? $this->serviceInterface : get_class($this->service));
	}

	/**
	 * Invokes the given method on the server.
	 * @param string $methodName
	 * @param Array $args
	 */
	public function invoke($methodName, Array &$args) {
		$method = $this->clazz->getMethod($methodName);
		if ($method->isAbstract() || !$method->isPublic()) {
			throw new Exception(
				"Service methods must be non static, non abstract and public");
		}
		return $method->invokeArgs($this->service, $args);
	}


	/**
	 * Sets the service interface.
	 * @param string $serviceInterface
	 */
	public function setServiceInterface($serviceInterface) {
		$this->serviceInterface = $serviceInterface;
	}

	/**
	 * Returns the service interface.
	 * @return the service interface
	 */
	protected function getServiceInterface() {
		return $this->serviceInterface;
	}

	/**
	 * Sets the service.
	 * @param string $service
	 */
	public function setService($service) {
		$this->service = $service;
	}

	/**
	 * Returns the service.
	 * @return the service
	 */
	protected function getService() {
		return $this->service;
	}
	
}

?>